﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
using System.Xml;

namespace Possan.Localization
{
	public class LibraryNamespace
	{
		Dictionary<string, LibraryPhraseInfo> phrases;
		Library library;

		public LibraryNamespace(Library lib)
		{
			library = lib;
			phrases = new Dictionary<string, LibraryPhraseInfo>();
		}

		public string GetPhrase(string originalphrase)
		{
			return GetPhrase(originalphrase, CultureInfo.CurrentUICulture);
		}

		public string GetPhrase(string originalphrase, CultureInfo locale)
		{
			return GetPhrase(originalphrase, locale.Name);
		}

		public string GetPhrase(string originalphrase, string locale)
		{
			if (phrases.ContainsKey(originalphrase))
			{
				LibraryPhraseInfo info = phrases[originalphrase];

				if (info.Translations.ContainsKey(locale))
					return info.Translations[locale];

				if (locale.Contains("-"))
				{
					string locale2 = locale.Substring(0, locale.IndexOf("-"));

					if (info.Translations.ContainsKey(locale2))
						return info.Translations[locale2];
				}
			}

			return originalphrase;
		}

		public string GetPhraseByID(string id, string originalphrase, string locale)
		{
			throw new NotImplementedException();
		}

		public bool RegisterPhrase(string originalphrase, string translation, CultureInfo locale)
		{
			return RegisterPhrase(originalphrase, translation, locale.Name);
		}

		public bool RegisterPhrase(string originalphrase, string translation, string locale)
		{
			library.RegisterLocale(library.DefaultLocale.Name);
			if (!string.IsNullOrEmpty(locale))
				library.RegisterLocale(locale);

			if (phrases.ContainsKey(originalphrase))
			{
				LibraryPhraseInfo info = phrases[originalphrase];
				if (!string.IsNullOrEmpty(translation) && !string.IsNullOrEmpty(locale))
				{
					if (info.Translations.ContainsKey(locale))
						info.Translations[locale] = translation;
					else
						info.Translations.Add(locale, translation);
				}
			}
			else
			{
				LibraryPhraseInfo info = new LibraryPhraseInfo();
				// info.ID = Guid.n
				if (!string.IsNullOrEmpty(translation) && !string.IsNullOrEmpty(locale))
				{
					info.Translations.Add(locale, translation);
				}
				phrases.Add(originalphrase, info);
			}

			return true;
		}

		public bool RegisterPhraseID(string id, string originalphrase, string translation, CultureInfo locale)
		{
			return RegisterPhraseID(id, originalphrase, translation, locale.Name);
		}

		public bool RegisterPhraseID(string id, string originalphrase, string translation, string locale)
		{
			library.RegisterLocale(library.DefaultLocale.Name);
			if (!string.IsNullOrEmpty(locale))
				library.RegisterLocale(locale);

			if (phrases.ContainsKey(originalphrase))
			{
				LibraryPhraseInfo info = phrases[originalphrase];
				if (!string.IsNullOrEmpty(translation) && !string.IsNullOrEmpty(locale))
				{
					if (info.Translations.ContainsKey(locale))
						info.Translations[locale] = translation;
					else
						info.Translations.Add(locale, translation);
				}
			}
			else
			{
				LibraryPhraseInfo info = new LibraryPhraseInfo();
				// info.ID = Guid.n
				if (!string.IsNullOrEmpty(translation) && !string.IsNullOrEmpty(locale))
				{
					info.Translations.Add(locale, translation);
				}
				phrases.Add(originalphrase, info);
			}

			return true;
		}

		public int PhraseCount
		{
			get
			{
				return phrases.Count;
			}
		}

		public List<string> GetIDs()
		{
			//	return phrases.Count;
		
			List<string> ids = new List<string>();
			
			Dictionary<string, LibraryPhraseInfo>.Enumerator pe = phrases.GetEnumerator();
			while (pe.MoveNext())
			{
				LibraryPhraseInfo info = pe.Current.Value;
				if( !string.IsNullOrEmpty(info.ID))
					ids.Add(info.ID);
			}

			return ids;
		}

		public void Clear()
		{
			phrases.Clear();
		}

		public void AppendXML(XmlNode rootnode)
		{

			Dictionary<string, LibraryPhraseInfo>.Enumerator pe = phrases.GetEnumerator();
			while (pe.MoveNext())
			{
				string phrase = pe.Current.Key;
				LibraryPhraseInfo info = pe.Current.Value;
				string phraseid = info.ID;

				if (string.IsNullOrEmpty(phraseid))
					phraseid = Guid.NewGuid().ToString();

				XmlNode pnode = rootnode.OwnerDocument.CreateElement("phrase");

				XmlNode idattr = rootnode.OwnerDocument.CreateElement("id");
				idattr.AppendChild(rootnode.OwnerDocument.CreateTextNode(phraseid));
				pnode.AppendChild(idattr);

				XmlNode originalattr = rootnode.OwnerDocument.CreateElement("orig");
				originalattr.AppendChild(rootnode.OwnerDocument.CreateTextNode(phrase));
				pnode.AppendChild(originalattr);

				rootnode.AppendChild(pnode);
				{
					foreach (LibraryPhraseFileReference fr in info.Refs)
					{
						XmlNode tnode = rootnode.OwnerDocument.CreateElement("ref");

						XmlAttribute fileattr = rootnode.OwnerDocument.CreateAttribute("file");
						fileattr.Value = fr.File;
						tnode.Attributes.Append(fileattr);

						XmlAttribute lineattr = rootnode.OwnerDocument.CreateAttribute("line");
						lineattr.Value = fr.Line.ToString();
						tnode.Attributes.Append(lineattr);

						pnode.AppendChild(tnode);
					}

					Dictionary<string, string>.Enumerator te = info.Translations.GetEnumerator();
					while (te.MoveNext())
					{
						//	info.Refs.GetEnumerator(

						XmlNode tnode = rootnode.OwnerDocument.CreateElement("tran");

						XmlAttribute langattr = rootnode.OwnerDocument.CreateAttribute("lang");
						langattr.Value = te.Current.Key;
						tnode.Attributes.Append(langattr);
						tnode.AppendChild(rootnode.OwnerDocument.CreateTextNode(te.Current.Value));
						
						pnode.AppendChild(tnode);
					}
				}
			}
		}

	}
}
